package com.pizza.abstrait;

import java.io.Serializable;
import java.rmi.RemoteException;

import javax.faces.context.FacesContext;

import com.pizza.control.ClientBean;
import com.pizza.control.CommandeBean;
import com.pizza.database.IPizzaManager;
import com.pizza.database.PizzaManagerClient;
import com.pizza.model.Adresse;

/**
 * Class AbstractAdresse, classe abstraite rassemblant toutes les informations d'une adresse client.
 *
 */
public abstract class AbstractAdresse implements Serializable
{
	private int id;

	private String adresse;

	private int cp;

	private String ville;

	private String telephone;

	public String getAdresse()
	{
		return adresse;
	}

	public void setAdresse(String adresse)
	{
		this.adresse = adresse;
	}

	public int getCp()
	{
		return cp;
	}

	public void setCp(int cp)
	{
		this.cp = cp;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getTelephone()
	{
		return telephone;
	}

	public void setTelephone(String telephone)
	{
		this.telephone = telephone;
	}

	public String getVille()
	{
		return ville;
	}

	public void setVille(String ville)
	{
		this.ville = ville.toUpperCase();
	}

	public void copyFrom(AbstractAdresse adresse)
	{
		setId(adresse.getId());
		setAdresse(adresse.getAdresse());
		setCp(adresse.getCp());
		setVille(adresse.getVille());
		setTelephone(adresse.getTelephone());
	}

	public void setForCommande()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		CommandeBean commande = (CommandeBean) context.getExternalContext()
				.getSessionMap().get("commandeBean");

		if (commande != null)
		{
			commande.setAdresseLivraison(new Adresse(this));
			commande.validerEtape2();
		}
	}

	public void remove()
	{
		IPizzaManager pizzaManager = PizzaManagerClient.GetPizzaManager();
		FacesContext context = FacesContext.getCurrentInstance();
		ClientBean client = (ClientBean) context.getExternalContext().getSessionMap()
				.get("clientBean");
		try
		{
			if (pizzaManager.removeAdresse(client, this))
				client.getAdressesLivraison().remove(this);
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
	}
}
